package com.tkx.dxb.service.article.controller.api;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tkx.dxb.common.base.result.R;
import com.tkx.dxb.service.article.entity.Comment;
import com.tkx.dxb.service.article.entity.vo.ApiCommentQueryVo;
import com.tkx.dxb.service.article.service.CommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author victor
 * @since 2021-04-07
 */
@Api(tags = "评论")
@RestController
@RequestMapping("/api/article/comment")
public class ApiCommentController {

    private CommentService commentService;

    @Autowired
    public void setTeacherService(CommentService commentService){
        this.commentService = commentService;
    }

    @ApiOperation("增加评论")
    @PostMapping("add-comment-info")
    public R add(
            @ApiParam(value = "添加评论", required = true)
            @RequestBody Comment comment) {
//        comment.setCommentLike(0);
        boolean result = commentService.save(comment);
        if (result){
            return R.ok().message("评论成功");
        } else{
            return R.ok().message("评论失败");
        }
    }



    @ApiOperation("评论分页")
    @GetMapping("comment-page/{page}/{limit}")
    public R getCommentPage(
            @ApiParam(value = "当前页码", required = true) @PathVariable Long page,
            @ApiParam(value = "每页记录数", required = true) @PathVariable Long limit,
            @ApiParam(value = "评论列表查询对象") ApiCommentQueryVo apiCommentQueryVo) {
        Page<Comment> pageParam = new Page<>(page, limit);
        /**
         * queryWrapper 查询条件，无查询对象省略
         */
        IPage<Comment> pageModel = commentService.selectApiPage(pageParam, apiCommentQueryVo);
        /**
         * getRecords   获取数据
         * getTotal     获取总页数
         */
        List<Comment> records = pageModel.getRecords();
        long total = pageModel.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }

}

